package com.carrental.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 数据库连接
 */
public class DBConnection {

	// 驱动类
	private static String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
	// 数据库IP地址
	private static String DB_IP = "localhost";
	// 数据库名
	private static String DB_NAME = "carrental";
	// 数据库用户名
	private static String DB_USERNAME = "root";
	// 数据库密码
	private static String DB_PASSWORD = "12345678";
	// 数据库连接地址
	private static String DB_URL = "jdbc:mysql://" + DB_IP + ":3306/" + DB_NAME + "?user=" + DB_USERNAME + "&password="
			+ DB_PASSWORD + "&useUnicode=true&characterEncoding=utf8";

	Connection connection = null;
	PreparedStatement preparedStatement = null;
	ResultSet resultSet = null;

	public DBConnection() throws Exception {
		connection = getConnection();
	}

	/**
	 * 获取数据库连接
	 * 
	 * @param server   数据库服务地址
	 * @param dbName   数据库名
	 * @param user     用户名
	 * @param password 密码
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	private Connection getConnection() throws Exception {

		// 注册驱动
		Class.forName(DB_DRIVER);

		// 获取数据库连接
		connection = DriverManager.getConnection(DB_URL);

		return connection;
	}

	/**
	 * 释放资源
	 */
	public void closeAll() {
		try {
			// 如果resultSet不空，关闭resultSet
			if (resultSet != null) {
				resultSet.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				// 如果preparedStatement不为空，关闭preparedStatement
				if (preparedStatement != null) {
					preparedStatement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				try {
					connection.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 执行查询语句
	 * 
	 * @param sql
	 * @return
	 * @throws SQLException
	 */
	public ResultSet executeQuery(String sql) throws SQLException {
		return executeQuery(sql, null);
	}

	/**
	 * 执行查询语句
	 * 
	 * @param sql   sql语句
	 * @param param 参数
	 * @return
	 */
	public ResultSet executeQuery(String sql, String[] param) throws SQLException {
		// 处理sql，执行sql
		// 得到PreparedStatement对象
		preparedStatement = connection.prepareStatement(sql);
		// 判断是否有参数
		if (preparedStatement != null && param != null) {
			for (int i = 0; i < param.length; i++) {
				// 为预编译的sql设置参数
				preparedStatement.setString(i + 1, param[i]);
			}
		}
		// 执行sql
		return preparedStatement.executeQuery();
	}

	/**
	 * 执行增删改查语句
	 * 
	 * @param sql
	 * @param param
	 * @return
	 */
	public int executeUpdate(String sql, String[] param) throws Exception {
		// 处理sql，执行sql
		// 得到PreparedStatement对象
		preparedStatement = connection.prepareStatement(sql);
		if (preparedStatement != null) {
			for (int i = 0; i < param.length; i++) {
				// 为预编译的sql设置参数
				preparedStatement.setString(i + 1, param[i]);
			}
		}
		// 执行sql
		return preparedStatement.executeUpdate();
	}

}
